<template>
	<div class="right-cps-view">
		<div class="tbs-view">
			<el-button v-for="(item, index) in tabs" :key="index" @click="activeBtn = index"
				:type="activeBtn == index ? 'primary' : ''"
				:disabled="(item.disabled ? true : false) || (index > 0 && ('listEmpty' == tableSelectRow && saveBTN))">
				{{ item.title }}
			</el-button>
			<!-- -弹框是否出现-{{ rightViewFlag }}--
			-是否修改-{{ DataChange }}-- -->
		</div>
		<!-- content -->
		<div class="container">
			<tabs-detail v-if="activeBtn == 0" :tableSelectRow="tableSelectRow" :Type="Type" @changeType="changeType"
				@changeBTN="changeBTN" @isDisabledFlgOne="isDisabledFlgOne" ref="tab1"
				@DataChangeFn="DataChangeFn"></tabs-detail>
			<tabs-plan v-if="activeBtn == 1" :tableSelectRow="tableSelectRow" ref="3"
				@DataChangeFn="DataChangeFn"></tabs-plan>
			<tabs-pic ref="tab3" v-show="activeBtn == 2" :tableSelectRow="tableSelectRow" @DataChangeFn="DataChangeFn"
				@isConfirmingFn="isConfirmingFn" :rightViewFlag="rightViewFlag"></tabs-pic>
			<tabs-tabel ref="tab4" v-show="activeBtn == 3" :tableSelectRow="tableSelectRow" @DataChangeFn="DataChangeFn"
				@isConfirmingFn="isConfirmingFn" :rightViewFlag="rightViewFlag"></tabs-tabel>
		</div>

		<!-- footer -->
		<footer class="footer-view">
			<!-- <button v-show="isBtnShow">Cancel</button> -->
			<el-button type="primary" v-show="$hasEditPermission('/BHView')" @click="_save(true)" :disabled="saveBTN">編集内容を保存</el-button>
			<el-button type="warning" @click="onCloseClick">
				{{ $t("message.public.close") }}
			</el-button>
		</footer>
	</div>
</template>

<script>
import tabs_detail from "./tabs/Tab_1.vue";
import tabs_plan from "./tabs/Tab_2.vue";
import tabs_tabel from "./tabs/Tab_4.vue";
import tabs_pic from "./tabs/Tab_3.vue";
import { getOfferCombox, offerDetiaRequiredMsgTips, delResOfferDetilEjectDataVerifyMsg, offerDetiaMsgTips, addResOfferDetiaEjectDataVerify, updResOfferDetialsEjectBookAssData } from "@/api/api";
import { getLocalStorage } from "@/utils/storage";
import tools from "@/utils/tools.js";
import eventBus from "@/eventBus/bus";



export default {
	name: "TabsCompontens",
	props: ["tableSelectRow", "rightViewFlag"],
	data() {
		return {
			//  数据是否修改过
			DataChange: false,
			isConfirming: false,

			tabs: [
				{ title: this.$t("message.rightDetailMessage.tabs.label_1") },
				{ title: this.$t("message.rightDetailMessage.tabs.label_2"), disabled: true },
				{ title: this.$t("message.rightDetailMessage.tabs.label_3"), disabled: true },
				{ title: this.$t("message.rightDetailMessage.tabs.label_4"), disabled: true },
			],
			activeBtn: 0,

			offerComboxData: [],
			Type: 2,
			saveBTN: true
		};
	},
	components: {
		"tabs-detail": tabs_detail,
		"tabs-plan": tabs_plan,
		"tabs-tabel": tabs_tabel,
		"tabs-pic": tabs_pic,
	},
	watch: {
		// '$store.state.isStop.name': function (nVal, oVal) {
		// 	//你需要执行的代码
		// 	console.log(nVal, oVal);
		// 	if (nVal == 'active') {
		// 		this._save();
		// 	}
		// },
		tableSelectRow: {
			deep: true,
			immediate: true,
			handler: function (newV, oldV) {
				if (this.activeBtn != 0) {
					this.activeBtn = 0;
					setTimeout(() => {
						this.$store.state.ISP_Info = {};
						this.$refs.tab1.getResOfferDetialEjectDataApi();
					}, 300);
				}
			},
		},

		macCodeValue: {
			deep: true,
			handler: function (newV, oldV) {
				if (this.activeBtn != 0) {
					this.activeBtn = 0;
					setTimeout(() => {
						this.$store.state.ISP_Info = {};
						this.$refs.tab1.getResOfferDetialEjectDataApi();
					}, 300);
				}
			},
		},
		activeBtn: {
			deep: true,
			handler: function (newV, oldV) {
				try {
					this.$refs.tab4.reSetTableFn();
				} catch (error) {
					//
				}
			},
		},
	},
	methods: {
		isConfirmingFn(e) {
			this.isConfirming = e;
		},

		DataChangeFn(e) {
			this.DataChange = e;
		},
		isDisabledFlgOne(e) {
			if (e == 1) {
				this.tabs[1].disabled = true;
				this.tabs[2].disabled = true;
				this.tabs[3].disabled = true;
			} else if (e == 0) {
				this.tabs[1].disabled = false;
				this.tabs[2].disabled = false;
				this.tabs[3].disabled = false;
			}
		},
		changeBTN(e) {
			this.saveBTN = e;
		},
		//2保存1新增
		changeType(e) {
			this.Type = e;
			console.log(this.Type)
		},
		_save(isSaveBtn) {
			// if (this.isConfirming) {
			// 	return
			// }
			this.isConfirming = true;
			// if (this.activeBtn == 2) {
			// 	this.$refs.3.saveFn(isSaveBtn);
			// 	return
			// }
			// if (this.activeBtn == 3) {
			// 	this.$refs.tab3.saveFn(isSaveBtn);
			// 	return
			// }
			console.log(this.Type)
			this.save(this.Type, isSaveBtn);
		},
		async save(type, isSaveBtn) {

			let tipMsg = await this.getMsg(type);
			this.$confirm(tipMsg, '', {
				confirmButtonText: 'OK',
				cancelButtonText: 'Cancel',
				type: 'warning'
			}).then(async () => {

				let countData = this.$store.state.ISP_Info;
				//必填项提示信息获取 1
				let getMSG = '';
				if (countData.preDate.trim() == '' || !countData.preDate) {
					getMSG = this.$t("message.rightDetailMessage.tab1.label_1")
				//} else if (countData.posName.trim() == '' || !countData.posName || countData.posNo == '' || !countData.posNo) {
                } else if (countData.posName.trim() == '' || !countData.posName || countData.posNo === '') { // posNo=0は許容するよう修正 20250130 hotfix
					getMSG = this.$t("message.rightDetailMessage.tab1.label_5")
				} else if (countData.ispSpcCode.trim() == '' || !countData.ispSpcCode) {
					getMSG = this.$t("message.rightDetailMessage.tab1.label_6")
				} else if (this.$store.state.macType == '2' && (countData.proNo.trim() == '' || !countData.proNo)) {
					getMSG = this.$t("message.rightDetailMessage.tab1.label_13")
				}


				if (countData.actukbn == '0') {
					if (countData.opeDate == '' || !countData.opeDate) {
						getMSG = this.$t("message.rightDetailMessage.tab1.label_15")
					}
				}
				let getMsgJson = { label: getMSG }
				if (getMSG != '') {
					let reqMSG = await this.offerDetiaRequiredMsgTipsApi(getMsgJson);
					this.$alert(reqMSG, {
						confirmButtonText: 'OK',
						type: 'warning',
					});
					//  导航条误
					eventBus.$emit('isNavChangeErrorFn');
					//  搜索框索引误
					eventBus.$emit('backParamsForLeftView');
					return
				}

				this.getCheched(type, isSaveBtn);

			}).catch(() => {
				// this.$store.commit('reSetIsStop');
				// eventBus.$emit('goNextOfLeftTable');
				// eventBus.$emit('LoadMSDetailTabelData', this.$store.state.ISP_Info.actuNo);
				if (isSaveBtn) {
					return
				}
				try {
					var tempFn = null;
					tempFn = this.$store.state.isCurrentFn.toString();

					this.$store.state.isCurrentNext = true;
					this.$store.commit('reSetIsStop');
					eventBus.$emit('LoadMSDetailTabelData', this.$store.state.ISP_Info.actuNo);
					eventBus.$emit("resetTabelStyleForMSD", true)
					this.GetOperationDetialsListApi();
				} catch (error) {
					//
				}
				
				if (tempFn.indexOf('ssqcop') != -1) {
					setTimeout(() => {
						this.$store.state.isCurrentFn();
						this.$store.commit("reSetIsCurrent");
					}, 2000);
				} else {
					this.$store.state.isCurrentFn();
					this.$store.commit("reSetIsCurrent");
				}
			});
		},
		// 预执行3
		getCheched(Type, isSaveBtn) {
			let dataJson1 = tools.changeNull(this.$store.state.ISP_Info);

			let dataJson = {
				// Type: 2,
				// DmgCode: this.$store.state.macCode,
				ActuNo: Number(this.$store.state.ISP_Info.actuNo) || 1069,
				opeDate: Number(this.$store.state.ISP_Info.actukbn) == 1 ? '' : this.$store.state.ISP_Info.opeDate,
				preDate: this.$store.state.ISP_Info.preDate,
				nextSsnDate: this.$store.state.ISP_Info.nextSsnDate,
				Actukbn: Number(dataJson1.actukbn),				//actukbn: 0px					int	实施区分 実施:0, 計画:1
				// actuNo: 1065,
			}
			addResOfferDetiaEjectDataVerify(dataJson).then((res) => {
				if (res.data.code == 0) {
					this.addResOfferDetilEjectDataApi(Type, isSaveBtn);
					return
				}
				if (res.data.data == 1) {
					this.$confirm(res.data.msg, '', {
						confirmButtonText: 'OK',
						cancelButtonText: 'cancel',
						type: 'warning'
					}).then(() => {

						this.addResOfferDetilEjectDataApi(Type, isSaveBtn);

					}).catch(() => {
					});
				}
				if (res.data.data == 2) {
					this.$confirm(res.data.msg, '', {
						confirmButtonText: 'OK',
						cancelButtonText: 'cancel',
						type: 'warning'
					}).then(() => {

						this.addResOfferDetilEjectDataApi(Type, isSaveBtn);

					}).catch(() => {
					});
				}
			})
		},
		// 获取提示信息2
		async getMsg(Type) {
			let result;
			let dataJson = {
				Type,
				DmgCode: this.$store.state.macCode,
				ActuNo: Number(this.$store.state.ISP_Info.actuNo) || 1069,
				actuKbn: Number(this.$store.state.ISP_Info.actukbn) || 0,
			}
			let res = await offerDetiaMsgTips(dataJson);
			if (res.data.code != 0) {
				return result = '登録します、宜しいですか？';
			}
			result = res.data.data;
			return result;
		},
		//必填项提示信息获取 1
		async offerDetiaRequiredMsgTipsApi(dataJson) {
			let res = await offerDetiaRequiredMsgTips(dataJson);
			if (res.data.code != 0) {
				return '必填项提示信息获取...'
			}
			return res.data.data;
		},


		// 执行编辑
		addResOfferDetilEjectDataApi(type, isSaveBtn) {
			let dataJson = tools.changeNull(this.$store.state.ISP_Info);

			let Json = {
				Type: type,				//								int	1=新增 2=修改
				MacType: Number(this.$store.state.macType),				//								int	设备类型 1=机器 2=配管
				DmgCode: Number(this.$store.state.macCode),//					int	机器番号
				Actukbn: Number(dataJson.actukbn),				//actukbn: 0px					int	实施区分 実施:0, 計画:1
				TCodName: dataJson.tCodName,	//tCodName: "外面腐食"			string	检査目的
				OpeDate: Number(dataJson.actukbn) == 1 ? '' : dataJson.opeDate,	//opeDate: "2009-11-12"			string	实施日
				SsnDate: dataJson.preDate,		//preDate: "2009-06"			string	予定日
				IspWayName: dataJson.ispWayName,		//ispWayName: "目視"			string	检査法案
				BfrComm: dataJson.bfrComm,		//bfrComm: "test"				string	实施理由
				ChgDate: dataJson.constructionDate,			//								string	建设年月			???计算-结果接口无次参数

				CntPln: dataJson.countermeasures,	//countermeasures: "33333333333" string	次回対策

				CrrDept: dataJson.crrDept,			//crrDept: ""					string	腐食深さ			???计算
				HeaThck: dataJson.heaThck,			//heaThck: ""					string	近傍肉厚			???计算
				IspMemb: dataJson.ispMemb,//ispMemb: "111111111111"		string	検査員
				IspSpcCode: dataJson.ispSpcCode,		//ispSpcCode: "外面"			string	检査面
				OPCodName: dataJson.opCodName,		//opCodName: "要"				string	開放要否

				OpnName: dataJson.nextOpnName,			//opnName: " "					string	(次回)開放要否		???No Data

				ConstructionPeriod: dataJson.constructionPeriod,//constructionPeriod: "日常"	string	実施時期

				NextOpeTrm: dataJson.nextOpeTrm,		//								string	(次回)実施時期		???No Data

				RCodName: dataJson.rCodName,			//remThck: ""					string	腐食系統			???No Data
				OCodName: dataJson.oCodName,	//oCodName: "一次検査"			string	作業区分
				ProNo: this.$store.state.macType == 1 ? ' ' : dataJson.proNo.toString(),		//proNo: "P-001-01"				string	ラインNo
				DeterMine: dataJson.deterMine,		//deterMine: "良好"				string	判定
				DpCodName: dataJson.dpCodName,		//dpCodName: "あり"				string	処置有無
				WOCodName: dataJson.woCodName,			//???NO Data 					string	検査方法1			???No Data
				WTCodName: dataJson.wtCodName,			//???NO Data 					string	検査方法1			???No Data
				WFCodName: dataJson.wfCodName,			//???NO Data 					string	検査方法1			???No Data
				Residual: Number(dataJson.residual),			//residual: "3"					string	余寿命評価 1=減肉(自動) 2=減肉以外(手動) 其他=未評価
				ThckCalcType: Number(dataJson.thckCalcType),		//thckCalcType: 0				string	計算方法			???计算
				CrrThckType: dataJson.crrThckType ? 1 : 0,		//crrThckType: 1				string	近傍肉厚使用		???计算

				WrkCls: dataJson.nextWrkCls,		//wrkCls: "二次検査"			string	(次回)作業区分		???--下面有一个(次回)作業区分

				ActuNo: Number(dataJson.actuNo) || 1069,			//actuNo: "33"					検査詳細NO,新增时，穿空，修改时，传页面上的actuNo

				NextSsnDate: dataJson.nextSsnDate,	//nextSsnDate: "2010-01"		string	次回计划年月		

				NextReply: dataJson.nextReply,			//nextReply: ""					string	次回 劝告

				LstLif: dataJson.remalife,				//								string	余寿命 				???计算-结果接口无次参数
				OddLif: dataJson.lifeReachesYear,				//								string	寿命 到达年			???计算-结果接口无次参数
				LstThk: dataJson.lstThck,				//								string	腐蝕率				???计算-结果接口无次参数
				MacCode: Number(this.$store.state.realMacCode),	//					string	ラインNo.
				MgtThck: dataJson.mgtThck,			//								是	string	管理値			???计算
				OrgThck: dataJson.orgThck,			//								string	元肉厚				???计算
				PltCode: Number(this.$store.state.pltCode),			//								string	装置Code	

				NextWrkCls: dataJson.nextWrkCls,			//								string	(次回)作業区分(51)	???--上面有一个(次回)作業区分

				PosName: dataJson.posName,		//posName: "ﾍﾞﾝﾄ"				 string	检査位置
				PosNo: Number(dataJson.posNo),			//posNo: 17	???					string	検査No.
				RemThck: dataJson.remThck,			//								string	残肉厚\				???计算
				Summary: dataJson.summary,	//summary: "222222222"			string	検査結果		
			}
			Json.OpeDateLab = this.$t("message.rightDetailMessage.tab1.label_15"); //实施日LAB
			Json.ChgDateLab = this.$t("message.rightDetailMessage.tab1.label_28"); //建设年月LAB
			Json.OrgThckLab = this.$t("message.bhView.table.column6"); //原肉厚LAB
			Json.MgtThckLab = this.$t("message.rightDetailMessage.tab1.label_34"); //管理值LAB
			Json.HeaThckLab = this.$t("message.rightDetailMessage.tab1.label_29"); //金榜肉厚LAB
			Json.CrrDeptLab = this.$t("message.rightDetailMessage.tab1.label_32"); //腐蚀深LAB
			Json.SrcRemThckLab = this.$t("message.rightDetailMessage.tab1.label_35"); //参肉厚LAB

			let DD = {
				addResOfferDetilEjectData: Json,
				updResOfferDetialsPhotoData: this.$refs.tab3.saveFn(),
				updResOfferDetialPhotoRelationData: this.$refs.tab4.saveFn(),
			}

			updResOfferDetialsEjectBookAssData(DD).then((res) => {
				if (res.data.code == -300 || res.data.code != 0) {
					this.$alert(res.data.msg, {
						confirmButtonText: 'OK',
						type: 'warning',
					})
					//  导航条误
					eventBus.$emit('isNavChangeErrorFn');
					//  搜索框索引误
					eventBus.$emit('backParamsForLeftView');
					return
				}

				if (res.data.code == 1) {
					this.$confirm(res.data.msg, '', {
						confirmButtonText: 'OK',
						showCancelButton: false,
					}).then(() => {
						//  导航条误
						eventBus.$emit('isNavChangeErrorFn');
						//  搜索框索引误
						eventBus.$emit('backParamsForLeftView');
					});
					return
				}

				try {
					var tempFn = null;
					tempFn = this.$store.state.isCurrentFn.toString();
				} catch (error) {
					//
				}

				// 更新成功后的操作
				this.$message.success(
					res.data.msg
				);

				/**
				 * new
				 */
				//	刷新？
				try {
					this.$store.state.isCurrentNext = true;
					this.$store.commit('reSetIsStop');
					eventBus.$emit('LoadMSDetailTabelData', res.data.data || this.$store.state.ISP_Info.actuNo);
					eventBus.$emit("resetTabelStyleForMSD", true)
					this.GetOperationDetialsListApi();
				} catch (error) {
					//
				}
				//	追加？
				if (tempFn.indexOf('ssqcop') != -1) {
					setTimeout(() => {
						this.$refs.tab1.fatherFn();
					}, 2000);
				}
				//	跳转？
				else if (!isSaveBtn) {
					this.$store.state.isCurrentFn()
					this.$store.commit("reSetIsCurrent");
				}
				return

				if (isSaveBtn) {
					this.$store.commit('reSetIsStop');
					eventBus.$emit('LoadMSDetailTabelData', res.data.data || this.$store.state.ISP_Info.actuNo);
					eventBus.$emit("resetTabelStyleForMSD", true)
					this.GetOperationDetialsListApi();
					return
				}
				// if (tempFn.indexOf('ssqcop') != -1) {
				// eventBus.$emit('LoadDetailTabelData');
				// eventBus.$emit("LoadMSDetailTabelData")
				// }

				setTimeout(() => {
					//	123123
					if (tempFn.indexOf('ssqcop') != -1) {
						this.$refs.tab1.fatherFn();
						return
					}
					//  
					this.$store.state.isCurrentFn()
					this.$store.commit("reSetIsCurrent");

					// eventBus.$emit('LoadMSDetailTabelData', res.data.data || this.$store.state.ISP_Info.actuNo);
					eventBus.$emit('LoadMSDetailTabelData');
					eventBus.$emit("resetTabelStyleForMSD", true)

				}, 2000);


			})
		},


		getOfferComboxApi() {
			getOfferCombox().then((res) => {
				this.offerComboxData = res.data.data;
				console.log(
					"getOfferComboxApi -=-=-=-=-= ",
					this.offerComboxData
				);
			});
		},

		onCloseClick() {
			this.$emit("onCloseClick", false);
		},
	},
	computed: {
		macCodeValue() {
			return this.$store.state.macCode;
		},
	},
	created() { },
	mounted() {
		eventBus.$on("MSDType", (type) => {
			this.Type = type
		})

		//  监听通知，是否有数据变更
		eventBus.$on('getBasicStyleCompontensCurrent_EventBus', () => {
			//	非此弹框，则不监听
			if (!this.rightViewFlag) {
				return
			}

			console.log('getBasicStyleCompontensCurrent_EventBus 333', '目视详细', !this.DataChange);

			// 获取当前数据是否更改。
			this.$store.commit("changeIsCurrentNext", !this.DataChange);
		});

		//  接受通知唤起保存弹框
		eventBus.$on('EventBusBasicStyleCompontensSaveFn', () => {
			//	非此弹框，则不监听
			if (!this.rightViewFlag) {
				return
			}

			if (!this.$store.state.isCurrentFn) {
				return
			}
			console.log('EventBusBasicStyleCompontensSaveFn 444', !this.$store.state.isCurrentFn);

			this._save();
		});
	},
	beforeDestroy() {
		eventBus.$off('getBasicStyleCompontensCurrent_EventBus');
		eventBus.$off('EventBusBasicStyleCompontensSaveFn');
	},
	destroyed() { },
};
</script>
<style lang="scss" scoped>
.right-cps-view {
	width: 100%;
	height: calc(100% - 2px);
	padding: 16px 0;
	border: 1px solid #c3c3c3;
	position: relative;
}

.tbs-view {
	width: 100%;
	height: 32px;
	display: flex;
	align-items: center;
	justify-content: flex-start;
	padding: 0 16px;

	button {
		width: 25% !important;
		height: 32px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0;
		border-radius: 0;
		// border: 1px solid #d3d3d3;
		// border-right: 0 none;
		// cursor: pointer;
		// background-color: #FFF;
	}

	// .activeBtn {

	// }
	// button:last-child {
	// 	border-right: 1px solid #d3d3d3;
	// }
	// .active-btn-style {
	// 	background: #106ebe;
	// 	color: #fff;
	// }
}

.container {
	width: 100%;
	height: calc(100% - 32px);
	overflow-y: scroll;
	padding: 12px 8px 32px 16px;
}

.footer-view {
	position: absolute;
	z-index: 10;
	bottom: 0;
	width: 100%;
	height: 47px;
	padding: 0 15px;
	border-top: 1px solid #d9d9d9;
	display: flex;
	align-items: center;
	background: #ffffff;
	justify-content: space-between;

	button {
		min-width: 82px;
		height: 32px;
		// background: #ffffff;
		// border: 1px solid #d9d9d9;
		margin-right: 12px;
		border-radius: 4px;
		border: 0;
		line-height: 0;
	}

	button:nth-child(1) {
		// background: #106ebe;
		// color: #fff;
		width: 129px !important;
		padding: 0 10px;
	}

	button:nth-child(2) {
		// position: absolute;
		// background: #eb7421;
		// color: #fff;
		right: 0px;
	}
}
</style>